//
//  MyWorkoutViewController.m
//  Bracelet
//
//  Created by jing zhao on 2/12/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//
#import "MyWorkoutViewController.h"
#import "HeadPageSwithView.h"
#import "APIConnection.h"
#import <AFNetworking/AFNetworking.h>

#import "TodayView.h"
#import "weekView.h"
#import "HistoryView.h"

#import "mycityView/Mycity_TodayView.h"
#import "mycityView/Mycity_WeekView.h"
#import "mycityView/Mycity_HistoryView.h"

#import "HSBC_TodayView.h"
#import "HSBC_Cities.h"
#import "HSBC_World.h"

#import "SynchronizationView.h"

#import "UIRefreshTableView.h"
#import "AppDelegate.h"
#import "AlertView.h"

#define STATUS_BAR_HEIGHT 20.0
@interface MyWorkoutViewController ()<FireUIPagedScrollViewDelegate,HeadPageSwitchDelegate>

@property (weak, nonatomic) IBOutlet UIButton*              ui_buttonTitle;
@property (weak, nonatomic) IBOutlet UILabel *              ui_labelTitleName;
@property (weak, nonatomic) IBOutlet HeadPageSwithView *    ui_myHeadpage;

@property (copy, nonatomic) NSString* strTitle;
@property (copy, nonatomic) NSString* strType;

@end

@implementation MyWorkoutViewController

-(void)scrollToIndex:(NSInteger)index
{
[self.ui_scrollView gotoPage:index animated:NO];
}

-(void)setTitle:(NSString *)title andwithType:(NSString*)type
{
    self.strTitle = title;
    self.strType = type;

}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.ui_labelTitleName.text = _strTitle;
    UIView *todayView,*weekView,*historyView;
    if ([_strType isEqualToString:STYPE_MYCITY]) {
        todayView = [[[NSBundle mainBundle]loadNibNamed:@"Mycity_TodayView" owner:self options:nil]lastObject];
        [(Mycity_TodayView*)todayView initWithType:_strType];
        weekView = [[[NSBundle mainBundle]loadNibNamed:@"Mycity_WeekView" owner:self options:nil]lastObject];
        [(Mycity_WeekView*)weekView initWithType:_strType];
        historyView = [[[NSBundle mainBundle]loadNibNamed:@"Mycity_HistoryView" owner:self options:nil]lastObject];
        [(Mycity_HistoryView*)historyView initWithType:_strType];
    }else if ([_strType isEqualToString:STYPE_HBCN]){
        todayView = [[[NSBundle mainBundle]loadNibNamed:@"HSBC_TodayView" owner:self options:nil]lastObject];
        [(HSBC_TodayView*)todayView initWithType:_strType];
        weekView = [[[NSBundle mainBundle]loadNibNamed:@"HSBC_Cities" owner:self options:nil]lastObject];
        [(HSBC_Cities*)weekView initWithType:_strType];
        historyView = [[[NSBundle mainBundle]loadNibNamed:@"HSBC_World" owner:self options:nil]lastObject];
        [(HSBC_World*)historyView initWithType:_strType];
    }else{
        todayView = [[[NSBundle mainBundle]loadNibNamed:@"TodayView" owner:self options:nil]lastObject];
        [(TodayView*)todayView initWithType:_strType];
        weekView = [[[NSBundle mainBundle]loadNibNamed:@"WeekView" owner:self options:nil]lastObject];
        [(WeekView*)weekView initWithType:_strType];
        historyView = [[[NSBundle mainBundle]loadNibNamed:@"HistoryView" owner:self options:nil]lastObject];
        [(HistoryView*)historyView initWithType:_strType];
    }
    [self.ui_scrollView addSubview:[[UIRefreshTableView alloc]initWithUiview:todayView ]];
    [self.ui_scrollView addSubview:[[UIRefreshTableView alloc]initWithUiview:weekView ]];
    [self.ui_scrollView addSubview:[[UIRefreshTableView alloc]initWithUiview:historyView ]];
    self.ui_scrollView.pagerDelegate = self;
    self.ui_scrollView.userInteractionEnabled = YES;
    self.ui_myHeadpage.m_delegate = self;
}

-(void)updateOneSectionData
{
    for (UIRefreshTableView *refreshView in self.ui_scrollView.subviews) {
        [refreshView updateNetwork];
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    for (UIRefreshTableView *refreshView in self.ui_scrollView.subviews) {
        [refreshView updateNetwork];
    }
}
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
  CGFloat width = [[UIScreen mainScreen] bounds].size.width;
  CGFloat height = [[UIScreen mainScreen] bounds].size.height;
  CGFloat tempHeight = height-75-10 -STATUS_BAR_HEIGHT;
  CGFloat tempWidth = width-20;
  
//self.ui_scrollView.frame.size.width
  [self.ui_scrollView setContentSize:CGSizeMake(tempWidth*3, tempHeight)];
    for (int i=0; i<3; i++) {
        UIView *view = [self.ui_scrollView.subviews objectAtIndex:i];
        view.frame = CGRectMake(tempWidth*i, 0, tempWidth, tempHeight);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/***********************************
 button did select
 **********************************/
- (IBAction)buttonWantChangeHome:(id)sender {
    if ([self.delegateChangeHomeController respondsToSelector:@selector(changeHomeController)])
        [self.delegateChangeHomeController changeHomeController];
}

#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

- (IBAction)buttonInfo:(id)sender {
    
    SynchronizationView *viewTemp = [SynchronizationView shareInstance];
     viewTemp = [[[NSBundle mainBundle]loadNibNamed:@"SynchronizationView" owner:self options:nil] lastObject];
    viewTemp.frame =  [UIScreen mainScreen].bounds;
//    AppDelegate *del = [[UIApplication sharedApplication] delegate];
    
    [ self.view  addSubview:viewTemp];

}


#pragma mark - deleage
-(void)firePagerChanged:(FireUIPagedScrollView*)pager pagesCount:(NSInteger)pagesCount currentPageIndex:(NSInteger)currentPageIndex
{
    [self.ui_myHeadpage setHeaderPageChangGoToButtonIndex:currentPageIndex];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION_BRACELET_ISFIRSTPAGE object:nil userInfo:@{NOTIFICATION_BRACELET_PAGE:[NSString stringWithFormat:@"%ld",(long)currentPageIndex]}];
}

//headerview Delegagte
-(void)delegateOnHeadPageSwitchButtonTap:(NSInteger)_integerSwitchButton
{
    [self.ui_scrollView gotoPage:_integerSwitchButton animated:YES];
}



@end
